package com.hrms.controller.admin;

import com.hrms.entity.Room;
import com.hrms.http.ResponseResult;
import com.hrms.service.RoomService;
import com.hrms.utils.EntityUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Transactional
@RestController
@RequestMapping("/admin/roommanage/editroom")
public class AdminEditRoomController {
    // 管理员客房信息修改

    @Autowired
    private RoomService roomService;

    @RequestMapping("/init")
    public ResponseResult<List<Map<String, Object>>> init() {
        // 初始化
        List<Map<String, Object>> list = roomService.selectAll();
        return new ResponseResult<>("SUCCESS", list);
    }

    @RequestMapping("priceInit")
    public ResponseResult<List<Map<String, Object>>> priceInit() {
        // 价格的初始化

        // 各客房类型的价格
        String standard_double_price = roomService.selectRoomPricebyType("标准大床房");
        String superior_double_price = roomService.selectRoomPricebyType("高级大床房");
        String business_double_price = roomService.selectRoomPricebyType("商务大床房");
        String standard_twin_price = roomService.selectRoomPricebyType("标准双床房");
        String superior_twin_price = roomService.selectRoomPricebyType("高级双床房");
        String business_twin_price = roomService.selectRoomPricebyType("商务双床房");
        String standard_price = roomService.selectRoomPricebyType("标准间");
        String standard_family_price = roomService.selectRoomPricebyType("标准家庭房");
        String superior_family_price = roomService.selectRoomPricebyType("高级家庭房");

        Map<String, Object> map = new HashMap<>();
        map.put("standard_double_price", standard_double_price);
        map.put("superior_double_price", superior_double_price);
        map.put("business_double_price", business_double_price);
        map.put("standard_twin_price", standard_twin_price);
        map.put("superior_twin_price", superior_twin_price);
        map.put("business_twin_price", business_twin_price);
        map.put("standard_price", standard_price);
        map.put("standard_family_price", standard_family_price);
        map.put("superior_family_price", superior_family_price);

        List<Map<String, Object>> list = new ArrayList<>();
        list.add(map);
        return new ResponseResult<>("SUCCESS", list);
    }

    @RequestMapping("/update")
    public ResponseResult<Object> update(@RequestBody EntityUtil entity) {
        // 修改客房信息
        Room room = new Room();
        room.setRoom_id(entity.getRoom_id());
        room.setRoom_type(entity.getRoom_type());
        room.setRoom_price(entity.getRoom_price());
        room.setRoom_status("空闲");

        // 检查相同类型的客房是否全都空闲
        Room roomBefore = new Room();
        roomBefore.setRoom_type(room.getRoom_type());

        if(!roomService.checkAllStatusByType(roomBefore)) {
            // 回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return new ResponseResult<>("ERROR", null);
        }

        // 修改指定房间
        if(!roomService.update(room)) {
            // 回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return new ResponseResult<>("ERROR", null);
        }

        // 修改相同客房类型的价格
        if(!roomService.updateAllPriceByType(room)) {
            // 回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return new ResponseResult<>("ERROR", null);
        }

        return new ResponseResult<>("SUCCESS", null);
    }

    @RequestMapping("delete")
    public ResponseResult<Object> delete(@RequestBody EntityUtil entity) {
        // 删除客房

        // 检查客房是否空闲
        Room roomBefore = new Room();
        roomBefore.setRoom_id(entity.getRoom_id());
        roomBefore.setRoom_status("空闲");

        if(!roomService.checkStatue(roomBefore)) {
            // 回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return new ResponseResult<>("ERROR", null);
        }

        // 删除客房
        // 如果存在相同房间号的已删除客房，则覆盖其room_type和room_price
        Room room = new Room();
        room.setRoom_id(entity.getRoom_id());
        room.setRoom_status("已删除");

        // 存在相同房间号的已删除客房
        if(roomService.checkStatue(room)) {
            room.setRoom_type(roomService.selectRoomType(room));
            room.setRoom_price(roomService.selectRoomPrice(room));

            // 覆盖
            if(!roomService.update(room)) {
                // 回滚
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return new ResponseResult<>("ERROR", null);
            }

            // 删除所要删除的客房记录
            room.setRoom_status("空闲");
            if(!roomService.delete(room)) {
                // 回滚
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return new ResponseResult<>("ERROR", null);
            }

            return new ResponseResult<>("SUCCESS", null);
        }

        // 不存在相同房间号的已删除客房
        // 修改客房状态
        Room roomAfter = new Room();
        roomAfter.setRoom_id(entity.getRoom_id());
        roomAfter.setRoom_status("已删除");

        if(!roomService.updateStatue(roomAfter)) {
            // 回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return new ResponseResult<>("ERROR", null);
        }

        return new ResponseResult<>("SUCCESS", null);
    }

    @RequestMapping("deleteBatch")
    public ResponseResult<Object> deleteBatch(@RequestBody EntityUtil entity) {
        // 批量删除客房

        Integer room_id_start = entity.getRoom_id_start();
        Integer room_id_end = entity.getRoom_id_end();

        for(int room_id = room_id_start; room_id <= room_id_end; room_id++) {
            // 检查客房是否空闲
            Room roomBefore = new Room();
            roomBefore.setRoom_id(String.valueOf(room_id));
            roomBefore.setRoom_status("空闲");

            if(!roomService.checkStatue(roomBefore)) {
                // 回滚
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return new ResponseResult<>("ERROR", null);
            }

            // 删除客房
            // 如果存在相同房间号的已删除客房，则覆盖其room_type和room_price
            Room room = new Room();
            room.setRoom_id(String.valueOf(room_id));
            room.setRoom_status("已删除");

            // 存在相同房间号的已删除客房
            if(roomService.checkStatue(room)) {
                room.setRoom_type(roomService.selectRoomType(room));
                room.setRoom_price(roomService.selectRoomPrice(room));

                // 覆盖
                if(!roomService.update(room)) {
                    // 回滚
                    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                    return new ResponseResult<>("ERROR", null);
                }

                // 删除所要删除的客房记录
                room.setRoom_status("空闲");
                if(!roomService.delete(room)) {
                    // 回滚
                    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                    return new ResponseResult<>("ERROR", null);
                }

                continue;
            }

            // 不存在相同房间号的已删除客房
            // 修改客房状态
            Room roomAfter = new Room();
            roomAfter.setRoom_id(String.valueOf(room_id));
            roomAfter.setRoom_status("已删除");

            if(!roomService.updateStatue(roomAfter)) {
                // 回滚
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return new ResponseResult<>("ERROR", null);
            }
        }

        return new ResponseResult<>("SUCCESS", null);
    }
}
